﻿Imports System.Windows.Forms

Public Class DataCutCreatorForm

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'validate
        'check to see if anything is selected

        Dim tn As TreeNode
        Dim tns As TreeNode
        Dim checked As Boolean = False

        For Each tn In TheView.Nodes
            For Each tns In tn.Nodes
                If tns.Checked Then
                    checked = True
                End If
            Next
        Next


        If Not checked Then
            MessageBox.Show("You have not selected any stages", "No stages selected", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If DataCutBox.SelectedValue <> "<New>" Then
            If MessageBox.Show("This will replace all current values in the selected data cut, are you sure you want to continue?", "Replace values?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DataCutCreatorForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadView()

        TypeBox.Text = "Learners"

        LoadDataCutBox()

    End Sub


    Private Sub LoadDataCutBox()
        Dim sql As String

        sql = "select '<New>' as DataCut_ID, '<New Data Cut>' as Description union select distinct DataCut_ID, Description from Modelling_" & TypeBox.Text & " where Source = 'DCC'"

        DataCutBox.DataSource = LoadDataView(sql)
        DataCutBox.DisplayMember = "Description"
        DataCutBox.ValueMember = "DataCut_ID"

    End Sub

    Private Sub LoadView()
        'load the view
        Dim BudgetID As String
        Dim ScenarioID As String

        With ModelForm
            BudgetID = .AllocationLinesGrid.Item(.AllocationLinesGrid.Columns("Budget_ID").Index, .AllocationLinesGrid.CurrentRow.Index).Value.ToString
            ScenarioID = .ScenarioBox.SelectedValue.ToString
        End With

        TheView.Nodes.Clear()

        Dim sql As String
        Dim TheData As DataView

        sql = "select ms.*, al.Allocation_Description from Modelling_Stages ms, Allocation_Lines al where al.Budget_ID = '" & BudgetID & "' and al.Allocation_ID = ms.Allocation_ID and ms.Scenario_ID = '" & ScenarioID & "' and al.Hidden = 0 order by ms.Allocation_ID, Stage_Number"

        TheData = LoadDataView(sql)

        Dim ALNode As TreeNode
        Dim StageNode As TreeNode
        Dim rc As DataRowView
        Dim CurrentID As String = ""

        For Each rc In TheData
            If CurrentID <> rc.Item("Allocation_ID").ToString Then
                ALNode = TheView.Nodes.Add(rc.Item("Allocation_ID").ToString, rc.Item("Allocation_Description").ToString, 0, 0)
            End If

            StageNode = ALNode.Nodes.Add(rc.Item("Stage_Number").ToString, rc.Item("Stage_Number").ToString & " - " & rc.Item("Stage_Description"), 1, 1)

            CurrentID = rc.Item("Allocation_ID").ToString
        Next
    End Sub

    Private Sub TheView_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TheView.AfterCheck
        If IsNothing(e.Node.Parent) Then
            'so check or uncheck all 
            Dim tn As TreeNode

            For Each tn In e.Node.Nodes
                tn.Checked = e.Node.Checked
            Next
        End If
    End Sub

    Private Sub TypeBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypeBox.SelectedIndexChanged
        LoadDataCutBox()

    End Sub
End Class
